package com.tarena.lbs.basic.service.impl;

import com.tarena.lbs.basic.dao.repository.AreaRepository;
import com.tarena.lbs.basic.pojo.vo.AreaVO;
import com.tarena.lbs.basic.service.AreaService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AreaServiceImpl implements AreaService {
    private final AreaRepository areaRepository;
    @Override
    public List<AreaVO> queryProvinces() {
        return areaRepository.queryProvinces();
    }
    @Override
    public List<AreaVO> queryChildren(Integer parentId) {
        return areaRepository.queryChildren(parentId);
    }
    @Override
    public List<AreaVO> tree() {
        List<AreaVO> provinceList = areaRepository.queryProvinces();
        provinceList.forEach(province -> {
            List<AreaVO> children = areaRepository.queryChildren(Math.toIntExact(province.getId()));
            children.forEach(child -> {
                child.setChildren(areaRepository.queryChildren(Math.toIntExact(child.getId())));
            });
            province.setChildren(children);
        });
        return provinceList;
    }
}
